/** ****************************************************************************
@addtogroup SHIP_RFIC_DRIVER
@{
*******************************************************************************/

#include "_mcu_spi.h"


std_err_t mcu_spi_init(
	mcu_spi_port_t *spi,
	uint16_t direction,
	uint16_t mode,
	uint16_t datasize,
	uint16_t cpol,
	uint16_t cpha,
	uint16_t nss,
	uint16_t baudrate_prescaler,
	uint16_t firstbit,
	uint16_t crc_polynomial)
{
	SPI_InitTypeDef param;
	SPI_StructInit(&param);
	// 填写参数
	param.SPI_Direction = direction;
	param.SPI_Mode = mode;
	param.SPI_DataSize = datasize;
	param.SPI_CPOL = cpol;
	param.SPI_CPHA = cpha;
	param.SPI_NSS = nss;
	param.SPI_BaudRatePrescaler = baudrate_prescaler;
	param.SPI_FirstBit = firstbit;
	param.SPI_CRCPolynomial = crc_polynomial;
	// 初始化
	SPI_Init((SPI_TypeDef*)spi, &param);
	// 使能 SPI
	SPI_Cmd((SPI_TypeDef*)spi, ENABLE);
	return STD_ERR_OK;
}

uint16_t mcu_spi_trx(mcu_spi_port_t *spi, uint16_t ch) {
    // 发送数据
	SPI_I2S_SendData((SPI_TypeDef*)spi, ch);
    // When BSY is set, the SPI is busy communicating.
	while(SPI_I2S_GetFlagStatus((SPI_TypeDef*)spi, SPI_I2S_FLAG_BSY) == SET);
    // 返回收到的数据
	return SPI_I2S_ReceiveData((SPI_TypeDef*)spi);
}


/// @}
